﻿/************************************************************************/
/*                                                                      */
/*                8051 assembly formatter                               */
/*                                                                      */
/*                                                                      */
/*                                                                      */
/*           My Blog:http://blog.csdn.net/akof1314                      */
/*                                                                      */
/*                                                                      */
/*                                                                      */
/*                                                                      */
/************************************************************************/

#pragma once
#include <cstring>
#include <string>
#include <algorithm>
#include <functional>
using namespace std;

class CAsmIndent
{
public:
	CAsmIndent(void);
	~CAsmIndent(void);
	void AsmTidyMain(const char *pSourceIn, string &pSourceOut, unsigned int nUpperLower = 0, unsigned int nTabSpace = 4);

private:
	void ToUpperOrLower(string &strLine, bool bUpper);
	void LineParse(string &strLine, string &pSourceOut, unsigned int nUpperLower);

	string::size_type m_LeftTabNum;
	string::size_type m_MidTabNum;
	string::size_type m_RightTabNum;
	unsigned int m_TabSpace;
};

inline int my_isspace( int ch )
{
	return (unsigned int)(ch - 9) < 5u  ||  ch == ' ';
}

inline string& lTrim(string &ss)
{
	string::iterator p = find_if(ss.begin(), ss.end(), not1(ptr_fun(my_isspace)));
	ss.erase(ss.begin(), p);
	return ss;
}

inline string& rTrim(string &ss)
{
	string::reverse_iterator p = find_if(ss.rbegin(), ss.rend(), not1(ptr_fun(my_isspace)));
	ss.erase(p.base(), ss.end());
	return ss;
}

inline string& trim(string &st)
{
	lTrim(rTrim(st));
	return st;
}